package com.jyw.business.obstacles.utils;

import cn.hutool.core.util.PhoneUtil;
import com.jyw.common.constant.ConfigKeyConstant;
import com.jyw.system.service.ISysConfigService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 沈峻
 * @ClassName PhoneUtils
 * @Description TODO
 * @Date 2023/7/18 9:14
 **/
@Component
public class PhoneUtils {

    @Resource
    private ISysConfigService configService;

    /**
     * 过滤来电前缀
     * @param phone
     * @return String
     * @author sunshine
     * @date 2022年12月8日
     */
    public String filterIncomingPrefix(String phone) {

        String LYLT_INTERNATIONAL_CODE = configService.selectConfigByKey(ConfigKeyConstant.INTERNATIONAL_CODE);
        String LYLT_AREA_CODE = configService.selectConfigByKey(ConfigKeyConstant.AREA_CODE);
        //去掉杠
        phone = phone.replace("-", "");
        //去掉前缀
        if(phone.indexOf(LYLT_INTERNATIONAL_CODE)==0&&LYLT_INTERNATIONAL_CODE.length()>0){
            phone = phone.substring(LYLT_INTERNATIONAL_CODE.length());
        }
        //判断是否手机号
        if (!PhoneUtil.isMobile(phone)) {
            //去掉地区号码
            if(phone.indexOf(LYLT_AREA_CODE)==0&&LYLT_AREA_CODE.length()>0){
                phone = phone.substring(LYLT_INTERNATIONAL_CODE.length());
            }
        }
        return phone;
    }
}
